# -*- coding: utf-8 -*-
# created on 2016/11/30
from mathsolver.functions.fushu.basic import fs_search_multiple
from mathsolver.functions.base import *
from mathsolver.functions.base.base import new_latex
from sympy import S, sqrt
from mathsolver.functions.fushu.base import Complex


# 求复数的实部
class FSReal(BaseFunction):
    """
    复数\\frac{-4i}{1+\\sqrt{3}i}的实部是()
    """
    def solver(self, *args):
        if isinstance(args[0], BaseComplexValue):
            target = args[0].sympify()
            key = list(target.keys())[0]
            values = target[key]
            reals = []
            for value in values:
                com = Complex(str(value))
                reals.append(com.real)
            if len(reals) == 1:
                real = reals[0]
                self.steps.append(["", "∴ %s的实部为: %s" % (new_latex(key), new_latex(real))])
                self.output.append(BaseNumber(real))
        elif isinstance(args[0], BaseVariable):
            target = args[0].sympify()
            values = self.search(target)
            reals = []
            for value in values:
                value_subs = fs_search_multiple(self.known, value)
                for value_sub in value_subs:
                    value = value.subs(value_sub)
                com = Complex(str(value))
                reals.append(com.real)
            if len(reals) == 1:
                real = reals[0]
                self.steps.append(["", "∴ %s的实部为: %s" % (new_latex(args[0].sympify()), new_latex(real))])
                self.output.append(BaseNumber(real))
        else:
            norm = args[0].value
            com = Complex(norm)
            real = com.real
            self.steps.append(["", "∴ %s的实部为：%s" % (com.print_complex(norm), new_latex(real))])
            self.output.append(BaseNumber(real))
        self.label.add("求复数的实部")
        return self


# 求复数的虚部
class FSImag(BaseFunction):
    """
    复数\\frac{-4i}{1+\\sqrt{3}i}的虚部是()
    """
    def solver(self, *args):
        if isinstance(args[0], BaseComplexValue):
            target = args[0].sympify()
            key = list(target.keys())[0]
            values = target[key]
            imags = []
            for value in values:
                com = Complex(str(value))
                imags.append(com.imag)
            if len(imags) == 1:
                imag = imags[0]
                self.steps.append(["", "∴ %s的虚部为: %s" % (new_latex(key), new_latex(imag))])
                self.output.append(BaseNumber(imag))
        elif isinstance(args[0], BaseVariable):
            target = args[0].sympify()
            values = self.search(target)
            imags = []
            for value in values:
                value_subs = fs_search_multiple(self.known, value)
                for value_sub in value_subs:
                    value = value.subs(value_sub)
                com = Complex(str(value))
                imags.append(com.imag)
            if len(imags) == 1:
                imag = imags[0]
                self.steps.append(["", "∴ %s的虚部为: %s" % (new_latex(args[0].sympify()), new_latex(imag))])
                self.output.append(BaseNumber(imag))
        else:
            norm = args[0].value
            com = Complex(norm)
            imag = com.imag
            self.steps.append(["", "∴ %s的虚部为：%s" % (com.print_complex(norm), new_latex(imag))])
            self.output.append(BaseNumber(imag))
        self.label.add("求复数的虚部")
        return self


# 复数为纯虚数
class FSHowPureImag(BaseFunction):
    """
    若复数\\frac{a + 3i}{1 + 2i}是纯虚数,a ∈  R,i为虚数单位,则实数a的值为()
    """
    def solver(self, *args):  # 虚部不为0，且实部为0 BaseEq, BasePoly
        real = args[0].sympify()
        imag = args[1].sympify()
        ineqs = [[imag, "!=", S.Zero], [real, S.Zero]]
        self.steps.append(["", "根据复数为纯虚数的条件, 得%"])
        self.steps.append(["", "%s" % BaseIneqs(ineqs).printing()])
        self.output.append(BaseIneqs(ineqs))
        self.label.add("复数为纯虚数条件")
        return self


# 复数为实数
class FSHowReal(BaseFunction):
    """
    如果复数(1+i)(1+mi)是实数,则实数m=().
    """
    def solver(self, *args):  # 虚部为0
        real, imag = args[0].value, args[1].value
        if len(args) > 2 and "非正" in args[2]:
            gen = BaseIneqs([[real, "<=", 0], [str(imag), 0]])
        elif len(args) > 2 and "非负" in args[2]:
            gen = BaseIneqs([[real, ">=", 0], [str(imag), 0]])
        elif len(args) > 2 and "正" in args[2]:
            gen = BaseIneqs([[real, ">", 0], [str(imag), 0]])
        elif len(args) > 2 and "负" in args[2]:
            gen = BaseIneqs([[real, "<", 0], [str(imag), 0]])
        else:
            gen = BaseEq([str(imag), 0])
        self.steps.append(["", "由题意得：%s" % gen.printing()])
        self.output.append(gen)
        self.label.add("复数为实数条件")
        return self


# 复数为虚数
class FSHowImag(BaseFunction):
    """
    如果复数(1+i)(1+mi)是虚数,则实数m的取值范围.
    """
    def solver(self, *args):  # 虚部不为0
        self.label.add("复数为虚数条件")
        real, imag = args[0].value, args[1].value
        gen = BaseIneq([str(imag), "!=", 0])
        self.steps.append(["", "为虚数的条件是：%s" % gen.printing()])
        self.output.append(gen)
        return self


# 求复数的模长
class FSMoChang(BaseFunction):
    """
    设复数z满足z(4-3i)=1,则z的模为().
    """
    def solver(self, *args):
        real = args[0].sympify()
        imag = args[1].sympify()
        length = sqrt(real ** 2 + imag ** 2)
        self.steps.append(["", "∴复数的模长为:%s" % (new_latex(length))])
        self.output.append(BaseNumber(length))
        self.label.add("求复数的模长")
        return self


# 求复数的共轭复数
class FSConjugate(BaseFunction):
    """
    复数\\frac{1-i}{1+i}的共轭复数是()
    """
    def solver(self, *args):
        if isinstance(args[0], BaseComplexValue):
            target = args[0].sympify()
            key = list(target.keys())[0]
            values = target[key]
            conjugates = []
            for value in values:
                com = Complex(str(value))
                conjugates.append(com.conjugate)
            if len(conjugates) == 1:
                conjugate = conjugates[0]
                self.steps.append(["", "∴ %s的共轭复数为: %s" % (new_latex(key), new_latex(conjugate))])
                self.output.append(BaseNumber(conjugate))
        elif isinstance(args[0], BaseVariable):
            values = self.search(args[0].sympify())
            conjugates = []
            for value in values:
                com = Complex(str(value))
                conjugates.append(com.conjugate)
            if len(conjugates) == 1:
                conjugate = conjugates[0]
                self.steps.append(["", "∴ %s的共轭复数为: %s" % (new_latex(args[0].sympify()), new_latex(conjugate))])
                self.output.append(BaseNumber(conjugate))
        else:
            value = args[0].sympify()
            com = Complex(value)
            conjugate = com.conjugate
            self.steps.append(["", "∴ %s的共轭复数为: %s" % (new_latex(value), Complex.print_complex(conjugate))])
            self.output.append(BaseNumber(conjugate))
        self.label.add("求复数的共轭复数")
        return self


# 复数所在的平面坐标
class FSPoint(BaseFunction):
    """
    设i是虚数单位,则复数\\dfrac{2i}{1-i}所对应的点位于第()象限
    """
    def solver(self, *args):
        real = args[0].sympify()
        imag = args[1].sympify()
        pt = BasePoint({"name": "", "value": [real, imag]})
        self.steps.append(["", "∴ 复数的平面坐标为：%s" % pt.printing()])
        self.output.append(pt)
        self.label.add("求复数的平面坐标")
        return self


# 两复数相等
class ComEq(BaseFunction):
    """
    已知a,b∈R, 且a-1+ai与3+2bi相等,则b=().
    """
    def solver(self, *args):
        assert len(args) == 2
        self.label.add("两复数相等")
        com_1, com_2 = Complex(args[0].value), Complex(args[1].value)
        gen = BaseEqs([[com_1.real, com_2.real], [com_1.imag, com_2.imag]])
        self.steps.append(["", "两复数相等的条件是：%s" % gen.printing()])
        self.output.append(gen)
        return self


class FSConjugates(BaseFunction):
    """
    a-2+bi与3a-i互为共轭复数,则实数a,b的值分别是
    """
    def solver(self, *args):
        real1 = args[0].sympify()
        image1 = args[1].sympify()
        real2 = args[2].sympify()
        image2 = args[3].sympify()
        self.steps.append(["", "由共轭复数的性质，知"])
        eqs = [[real1, real2], [image1, -image2]]
        self.steps.append(["", BaseEqs(eqs).printing()])
        self.output.append(BaseEqs(eqs))
        self.label.add("根据共轭复数性质列等式")
        return self


if __name__ == '__main__':
    pass
